package com.cjg.scrolldemo;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnLoadMoreListener;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.cjg.scrolldemo.adapter.EmptyViewAdapter;
import com.cjg.scrolldemo.adapter.LoadMoreAdapter;
import com.cjg.scrolldemo.databinding.ActivityRecyclerviewBinding;
import com.cjg.scrolldemo.databinding.ActivityScrollingBinding;
import com.cjg.scrolldemo.databinding.ItemRecyclerviewBinding;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class RecyclerViewActivity extends AppCompatActivity{
    private static final String TAG = "~RecyclerViewActivity";
    private LoadMoreAdapter<String, BaseDataBindingHolder<ItemRecyclerviewBinding>> adapter;
    private RecyclerView rv;

//    private ActivityRecyclerviewBinding binding;





    @SuppressLint("ResourceAsColor")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        binding = ActivityRecyclerviewBinding.inflate(getLayoutInflater());
//        setContentView(binding.getRoot());
        setContentView(R.layout.activity_recyclerview);

        rv=findViewById(R.id.rv);

        rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        new PagerSnapHelper().attachToRecyclerView(rv);

//        MyAdapter adapter = new MyAdapter(getArrayList());
        adapter=new LoadMoreAdapter<String, BaseDataBindingHolder<ItemRecyclerviewBinding>>(R.layout.item_recyclerview,getArrayList()){
            protected void convert(BaseDataBindingHolder<ItemRecyclerviewBinding> holder, String s) {
                ItemRecyclerviewBinding itemBindding=holder.getDataBinding();
                itemBindding.tvTitle.setText("DB:"+s);
//                Glide.with(mContext).load(item.getUserAvatar()).crossFade().into((ImageView) viewHolder.getView(R.id.iv));
            }
        };
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.e(TAG, "onItemClick:"+position);
                Toast.makeText(RecyclerViewActivity.this, "点击了第" + (position + 1) + "条条目", Toast.LENGTH_SHORT).show();
            }

        });
        adapter.setEmptyView(R.layout.view_empty);

        adapter.addChildClickViewIds(R.id.cardview);// 先注册需要点击的子控件id（注意，请不要写在convert方法里）
        // 设置子控件点击监听
        adapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                if (view.getId() == R.id.cardview) {
                    Toast.makeText(RecyclerViewActivity.this, "onItemChildClick " + position, Toast.LENGTH_SHORT).show();
                }
            }
        });
        rv.setAdapter(adapter);
        rv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                Log.e(TAG, "onFocusChange: "+v.getId());
            }
        });

//        adapter.getLoadMoreModule();
//        //上拉加载（设置这个监听就表示有上拉加载功能了）
//        adapter.getLoadMoreModule().setOnLoadMoreListener(new OnLoadMoreListener() {
//            @Override
//            public void onLoadMore() {
//                Toast.makeText(RecyclerViewActivity.this, "onLoadMoreRequested", Toast.LENGTH_SHORT).show();
////                mRecyclerView.postDelayed(new Runnable() {
////                    @Override
////                    public void run() {
////                        if (mCurrentCounter >= TOTAL_COUNTER) {
////                            //数据全部加载完毕
////                            mQuickAdapter.loadMoreEnd();
////                        } else {
////                            if (isErr) {
////                                //成功获取更多数据（可以直接往适配器添加数据）
////                                mQuickAdapter.addData(DataServer.getSampleData(PAGE_SIZE));
////                                mCurrentCounter = mQuickAdapter.getData().size();
////                                //主动调用加载完成，停止加载
////                                mQuickAdapter.loadMoreComplete();
////                            } else {
////                                //获取更多数据失败
////                                isErr = true;
////                                Toast.makeText(PullToRefreshUseActivity.this, R.string.network_err, Toast.LENGTH_LONG).show();
////                                //同理，加载失败也要主动调用加载失败来停止加载（而且该方法会提示加载失败）
////                                mQuickAdapter.loadMoreFail();
////
////                            }
////                        }
////                    }
////
////                }, delayMillis);
//            }
//        });

////        adapter.setPreLoadNumber(3);
////        adapter.disableLoadMoreIfNotFullPage();
//
//        adapter.setUpFetchEnable(true);
//        adapter.setUpFetchListener(new BaseQuickAdapter.UpFetchListener() {
//            @Override
//            public void onUpFetch() {
//                adapter.setUpFetching(true);
//                Toast.makeText(RecyclerViewActivity.this, "onUpFetch", Toast.LENGTH_SHORT).show();
//            }
//        });


//        SwipeRefreshLayout mSwipeRefreshLayout=findViewById(R.id.swiperefreshlayout);
//        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
////                mSwipeRefreshLayout.setRefreshing(false);//刷新旋转动画停止
//                Toast.makeText(RecyclerViewActivity.this, "onRefresh", Toast.LENGTH_SHORT).show();
//            }
//        });
//
//        mSwipeRefreshLayout.setColorScheme(
////                android.R.color.holo_blue_bright,
////                android.R.color.holo_green_light,
////                android.R.color.holo_red_light,
//                android.R.color.holo_orange_light
//        );
//        mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(android.R.color.holo_blue_bright);

        SmartRefreshLayout mSmartRefreshLayout=findViewById(R.id.smartrefreshlayout);
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                Toast.makeText(RecyclerViewActivity.this, "onRefresh", Toast.LENGTH_SHORT).show();// mSmartRefreshLayout.finishRefresh();//mSmartRefreshLayout.finishLoadMore();
            }
        });

    }
    private View getErrorView() {
        View errorView = getLayoutInflater().inflate(R.layout.view_error, rv, false);
        errorView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                onRefresh();
            }
        });
        return errorView;
    }

    //模拟数据
    private List<String> getArrayList() {
        List<String> dataList = new ArrayList<>();
        for (int n = 0; n < 4; n++) {
            dataList.add("item position = " + n);
        }
        return dataList;
    }

    public static class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
        private List<String> dataList;

        MyAdapter(List<String> dataList) {
            this.dataList = dataList;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recyclerview, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            String content = dataList.get(position);
            holder.tv.setText(content);
        }

        @Override
        public int getItemCount() {
            return dataList == null ? 0 : dataList.size();
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            private TextView tv;

            ViewHolder(@NonNull View itemView) {
                super(itemView);
                tv = itemView.findViewById(R.id.tv);
            }
        }
    }

    @Override
    public void onBackPressed() {
//        adapter.addData(getArrayList());
//        adapter.loadMoreComplete();
//        adapter.loadMoreFail();
//        adapter.loadMoreEnd();
        Log.e(TAG, "onBackPressed: "+TextUtils.isEmpty(""));

        Log.e(TAG, "onBackPressed: "+"".split(",").length);
        Log.e(TAG, "onBackPressed: "+"".split(",").toString());
        Log.e(TAG, "onBackPressed: "+ Arrays.asList("".split(",")).size());
    }


}